package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TextView;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.fragment.task.TaskFragment1;
import com.wnhz.work.yz.fragment.task.TaskFragment2;
import com.wnhz.work.yz.fragment.task.TaskFragment3;
import com.wnhz.work.yz.fragment.task.TaskFragment4;
import com.wnhz.work.yz.fragment.task.TaskFragment5;

import java.util.ArrayList;
import java.util.List;

public class TaskAllActivity extends BaseActivity implements View.OnClickListener {

    private List<Fragment> fragmentList;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;

    private TaskFragment1 fragment1;
    private TaskFragment2 fragment2;
    private TaskFragment3 fragment3;
    private TaskFragment4 fragment4;
    private TaskFragment5 fragment5;
    private TextView title1, title2, title3, title4, title5;
    private View line1, line2, line3, line4, line5;
    private int textColorNor, textColorCli;
    private List<View> lines = new ArrayList<>();
    private List<TextView> titles = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_all);
        initData();
        initTitle();
        initView();
    }

    private void initView() {
        textColorNor = getResources().getColor(R.color.text_two);
        textColorCli = getResources().getColor(R.color.main_color);

        findViewById(R.id.shop_title1).setOnClickListener(this);
        findViewById(R.id.shop_title2).setOnClickListener(this);
        findViewById(R.id.shop_title3).setOnClickListener(this);
        findViewById(R.id.shop_title4).setOnClickListener(this);
        findViewById(R.id.shop_title5).setOnClickListener(this);
        title1 = (TextView) findViewById(R.id.shop_title1);
        title2 = (TextView) findViewById(R.id.shop_title2);
        title3 = (TextView) findViewById(R.id.shop_title3);
        title4 = (TextView) findViewById(R.id.shop_title4);
        title5 = (TextView) findViewById(R.id.shop_title5);
        line1 = findViewById(R.id.line1);
        line2 = findViewById(R.id.line2);
        line3 = findViewById(R.id.line3);
        line4 = findViewById(R.id.line4);
        line5 = findViewById(R.id.line5);
        fragmentList = new ArrayList<>();
        fragment1 = new TaskFragment1();
        fragment2 = new TaskFragment2();
        fragment3 = new TaskFragment3();
        fragment4 = new TaskFragment4();
        fragment5 = new TaskFragment5();
        fragment1.setType(type);
        fragment2.setType(type);
        fragment3.setType(type);
        fragment4.setType(type);
        fragment5.setType(type);
        fragment1.setUser_id(user_id);
        fragment2.setUser_id(user_id);
        fragment3.setUser_id(user_id);
        fragment4.setUser_id(user_id);
        fragment5.setUser_id(user_id);
        fragment1.setContext(this);
        fragment2.setContext(this);
        fragment3.setContext(this);
        fragment4.setContext(this);
        fragment5.setContext(this);
        fragmentList.add(fragment1);
        fragmentList.add(fragment2);
        fragmentList.add(fragment3);
        fragmentList.add(fragment4);
        fragmentList.add(fragment5);
        lines.add(line1);
        lines.add(line2);
        lines.add(line3);
        lines.add(line4);
        lines.add(line5);
        titles.add(title1);
        titles.add(title2);
        titles.add(title3);
        titles.add(title4);
        titles.add(title5);
        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.fl_shopping_card, fragment1);
        transaction.add(R.id.fl_shopping_card, fragment2);
        transaction.add(R.id.fl_shopping_card, fragment3);
        transaction.add(R.id.fl_shopping_card, fragment4);
        transaction.add(R.id.fl_shopping_card, fragment5);
        transaction.hide(fragment2);
        transaction.hide(fragment3);
        transaction.hide(fragment4);
        transaction.hide(fragment5);
        transaction.commit();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.ll_search:
                startActivity(new Intent(this,SearchTaskActivity.class).putExtra("type",type+""));
                break;
            case R.id.shop_title1:
                hideFragment(0);
                break;
            case R.id.shop_title2:
                hideFragment(1);
                break;
            case R.id.shop_title3:
                hideFragment(2);
                break;
            case R.id.shop_title4:
                hideFragment(3);
                break;
            case R.id.shop_title5:
                hideFragment(4);
                break;


        }

    }

    private void hideFragment(int index) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        for (int i = 0; i < fragmentList.size(); i++) {
            if (i == index) {
                transaction.show(fragmentList.get(i));
                titles.get(i).setTextColor(textColorCli);
                lines.get(i).setVisibility(View.VISIBLE);
            } else {
                transaction.hide(fragmentList.get(i));
                titles.get(i).setTextColor(textColorNor);
                lines.get(i).setVisibility(View.INVISIBLE);
            }

        }
        transaction.commit();
    }

    private String titleStr = "";
    private int type = 0;
    private String user_id = "";

    private void initData() {
        titleStr = getIntent().getStringExtra("title");
        type = getIntent().getIntExtra("type", 0);
        user_id = getIntent().getStringExtra("user_id");
    }

    private void initTitle() {
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText(titleStr);
        title.setTextColor(getResources().getColor(R.color.main_color));
        //title.setTextSize(18);
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.ll_search).setOnClickListener(this);
        if (user_id != null && !"".equals(user_id)) {
        } else
            findViewById(R.id.ll_search).setVisibility(View.VISIBLE);
    }


}
